package com.zl.smartmall.library.listener;


import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.po.ShoppingCartInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 购物车删除商品数据监听类
 * @author 陈希然
 *
 */
public abstract class DeleteGoodsListener extends BaseListener {


	private static final String TAG = DeleteGoodsListener.class.getSimpleName();

	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		
			try {
				int status = response.getInt(Constants.REQ_STATUS);
				String msg = response.getString(Constants.REQ_MSG);
				CrashHandler.d(TAG, "购物车删除商品服务器status = " + status);
				CrashHandler.d(TAG, "购物车删除商品服务器msg = " + msg);

				switch(status){
				case Constants.REQ_OK:
					JSONArray jsonArray = response.getJSONArray(Constants.REQ_RESULT);
					List<ShoppingCartInfo> shoppingCartInfos = new ArrayList<ShoppingCartInfo>();
					for(int i = 0;i<jsonArray.length();i++){
						ShoppingCartInfo info = new ShoppingCartInfo();
						JSONObject productJson = jsonArray.getJSONObject(i);
						info.setProductId(productJson.getInt("product_id"));
						info.setShopId(productJson.getInt("shop_id"));
						info.setBuyNum(productJson.getInt("buy_num"));
						info.setActId(productJson.getInt("act_id"));
						info.setSeleted(false);
						shoppingCartInfos.add(info);
					}
					onResponseSuccess(shoppingCartInfos);
					break;
				
				case Constants.REQ_OFFLINE:
					onOffline();
					break;
					
				case Constants.REQ_NO_MORE_DATA:
					onNoMoreData();
					break;
					
				default:
					onResponseFailed();
				}
				
			} catch (JSONException e) {
				onFailure(statusCode, headers, e, new JSONObject());
			}
				
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
	}
	
	public abstract void onResponseSuccess(List<ShoppingCartInfo> shoppingCartInfos);

}
